package com.cinema;
import java.util.Scanner;

public class Main {
    private static User loginUser = null;

    public static void main(String[] args) {
        DataManager.init();

        Scanner scanner = new Scanner(System.in);
        while (true) {
            if (loginUser == null) {
                showLoginMenu(scanner);
            } else {
                showMainMenu(scanner);
            }
        }
    }

    private static void showLoginMenu(Scanner scanner) {
        System.out.println("\n=== 万东电影院管理系统 ===");
        System.out.println("1. 登录");
        System.out.println("2. 退出系统");
        System.out.print("请选择：");
        int choice = scanner.nextInt();
        scanner.nextLine();

        if (choice == 1) {
            System.out.print("用户名：");
            String username = scanner.nextLine();
            System.out.print("密码：");
            String password = scanner.nextLine();
            login(username, password);
        } else if (choice == 2) {
            DataManager.saveData();
            System.out.println("系统已退出");
            System.exit(0);
        }
    }

    private static void login(String username, String password) {
        String encryptedPwd = MD5Util.encrypt(password);
        for (User user : DataManager.userList) {
            if (user.getUsername().equals(username) && user.getPassword().equals(encryptedPwd)) {
                loginUser = user;
                System.out.println("登录成功！欢迎回来，" + username);
                return;
            }
        }
        System.out.println("用户名或密码错误");
    }

    private static void showMainMenu(Scanner scanner) {
        System.out.println("\n=== 功能菜单 ===");
        if (loginUser instanceof Admin) {
            showAdminMenu(scanner);
        } else if (loginUser.getUserType().equals("经理")) {
            showManagerMenu(scanner);
        } else if (loginUser.getUserType().equals("前台")) {
            showFrontMenu(scanner);
        }
    }

    private static void showAdminMenu(Scanner scanner) {
        System.out.println("1. 修改自身密码");
        System.out.println("2. 重置用户密码");
        System.out.println("3. 用户管理");
        System.out.println("4. 退出登录");
        System.out.print("请选择：");
        int choice = scanner.nextInt();
        scanner.nextLine();

        if (choice == 4) {
            loginUser = null;
            System.out.println("已退出登录");
        }

    }

    private static void showManagerMenu(Scanner scanner) { /* 功能实现 */ }
    private static void showFrontMenu(Scanner scanner) { /* 功能实现 */ }
}